//
//  NewAlbumViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/17.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "NewAlbumViewController.h"

@interface NewAlbumViewController ()
{
    NSMutableArray *_ArrOfmusicID;
    NSMutableArray *_DetailArr;
    NSMutableArray *url_arr;
    
}
@end

@implementation NewAlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = @"最新专辑";
    self.navigationItem.titleView = self.TitleView;
    
    _ArrOfmusicID = [[NSMutableArray alloc] init];
    _DetailArr =  [[NSMutableArray alloc] init];
    url_arr = [[NSMutableArray alloc] init];
    
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;
    [_normalView getURLForImage:self.recommedModel.pic textForLabel:self.recommedModel.tweet];
    UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    footer.backgroundColor = [UIColor lightGrayColor];
    footer.alpha = 0.3;
    _normalView.tableView.tableFooterView = footer;
    [self.view addSubview:_normalView];   
    
    
    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.removeFromSuperViewOnHide = YES;
    HUD.labelText = @"loading...";
    [self.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    
    [self getDataForSinger];
    
    BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
    [self.view addSubview:BarPlayer];
    
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    

    
    // Do any additional setup after loading the view.
}


- (void)getDataForSinger
{
    
    /**
     *  图片
     */
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *one_string = @"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B";
    NSString *url_string = [NSString stringWithFormat:@"%@%@", one_string, self.recommedModel._id];
    
  
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSArray *dataArr = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in dataArr) {
            NSArray *songListArr = [dic objectForKey:@"songlist"];
            for (NSDictionary *littleDic in songListArr) {
                NSString *song_id = [littleDic objectForKey:@"song_id"];
                [_ArrOfmusicID addObject:song_id];
            }
        }
        NSMutableString *szMuMyString = [NSMutableString stringWithString:@""];
        for (int i = 0; i< _ArrOfmusicID.count; i++) {
            NSString *kk = [_ArrOfmusicID objectAtIndex:i];
            NSString *kkkk = [NSString stringWithFormat:@"%@," ,kk];
            [szMuMyString appendString:kkkk];
        }
        
        /**
         *  音乐数据
         */
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *url_string = [NSString stringWithFormat:@"http://ting.hotchanson.com/songs/downwhite?song_id=%@", szMuMyString];
        AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/x-javascript",nil];
        [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
            
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                ReDetailModel *reDetail = [[ReDetailModel alloc] init];
                [reDetail setValuesForKeysWithDictionary:dic];
                
                [_DetailArr addObject:reDetail];
            }
            
            
            [HUD removeFromSuperview];
            _normalView.tableView.separatorColor = [UIColor lightGrayColor];
            [_normalView.tableView reloadData];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"失败==== %@",error);
        }];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DetailArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    ReDetailModel *redetail = [_DetailArr objectAtIndex:indexPath.row];
    [cell getDataForCell:redetail];
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];    
    return cell;
    
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReDetailModel *reDetailmodel = [_DetailArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _DetailArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel index:indexPath.row];
    
    
}


// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{
    if (model.audition_list.count > 1) {
        NSDictionary *dic = [model.audition_list objectAtIndex:1];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
        
    } else {
        NSDictionary *dic = [model.audition_list objectAtIndex:0];
        NSString *url_string = [dic objectForKey:@"url"];
        [Legacy play:url_string];
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*ADJUSTWIDTH;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
